package com.zhxl.haha;

import java.io.IOException;
import java.io.InputStream;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.Toast;


public class MainActivity extends Activity implements OnClickListener {
	private WebView webview;
	private ImageView imageView;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.image);
		imageView = (ImageView) findViewById(R.id.image);
		Resources rs = getResources();
		AssetManager am  = rs.getAssets();
		try {
			new Thread(){
				public void run(){
					try {
						Thread.sleep(2000);
					} catch (InterruptedException e) {
					}
				}
			}.start();
			//缓存问题处理图片
			InputStream is = am.open("oschina.jpg");
			/***
			 * 这地方需要写上jpg后最名称，否则IS为空
			 * oschina.jpg文件图片放在了Assert工程文件夹内了
			 */
			Bitmap bm = BitmapFactory.decodeStream(is);
			imageView.setImageBitmap(bm);
			imageView.setOnClickListener(this);
		} catch (IOException e) {
			System.out.println();
		}
		String name = rs.getString(R.id.spring);
//		System.out.println(rs==null);
		//Uri uri = Uri.parse("http://www.baidu.com");
		//Intent intent = new Intent(Intent.ACTION_VIEW,uri);
	     //startActivity(intent);  
//		 
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menutest, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		int itemId = item.getItemId();
		switch(itemId){
		case R.id.m1:
			webview = new WebView(this);
	        //设置WebView属性，能够执行Javascript脚本  
	        webview.getSettings().setJavaScriptEnabled(true);  
	        //加载需要显示的网页  
	        webview.loadUrl("http://user.qzone.qq.com/menutest/infocenter");  
	        //设置Web视图  
	      //覆盖WebView默认使用第三方或系统默认浏览器打开网页的行为，使网页用WebView打开
	        webview.setWebViewClient(new WebViewClient(){
	            @Override
	         public boolean shouldOverrideUrlLoading(WebView view, String url) {
	                //返回值是true的时候控制去WebView打开，为false调用系统浏览器或第三方浏览器
	              view.loadUrl(url);
	             return true;
	         }
	        });
	        setContentView(webview); 
	        return super.onOptionsItemSelected(item);
		case R.id.m2:
			Builder builder = new AlertDialog.Builder(this);
			builder.setTitle("选择的是右边菜单").setMessage("确认关闭吗");
			builder.setPositiveButton("确定",new DialogInterface.OnClickListener(){
				@Override
				public void onClick(DialogInterface dialog, int which) {
					Toast.makeText(MainActivity.this,"右边菜单",Toast.LENGTH_LONG).show();
				}
			});
			builder.setNegativeButton("取消",new DialogInterface.OnClickListener(){
				@Override
				public void onClick(DialogInterface dialog, int which) {
					Toast.makeText(MainActivity.this,"取消",Toast.LENGTH_LONG).show();
				}});
			builder.show();
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
//		if ((keyCode == KeyEvent.KEYCODE_BACK)) {  
//            //webview.goBack(); //goBack()表示返回WebView的上一页面  
//            return true;  
//        }
		if ((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()) {  
            webview.goBack(); //goBack()表示返回WebView的上一页面  
            return true;  
        }  
		return super.onKeyDown(keyCode, event);
	}

	@Override
	public void onClick(View v) {
		Toast.makeText(MainActivity.this,"切换",Toast.LENGTH_LONG).show();
		Intent i = new Intent();
		i.setClass(MainActivity.this,LoginActivity.class);
		startActivity(i);
	}
}
